package fl.ass.service.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.project.constant.PrjBscInfoConstant;
import fl.ass.facade.api.IAssSchFeeDFlowFactoryApi;
import fl.ass.facade.constant.AssFileConstant;
import fl.ass.facade.constant.InterfaceConstants;
import fl.ass.service.dao.AssSchFeeDMapper;
import fl.ass.service.po.AssSchFeeM;
import fl.ass.service.po.PrjPrdDtl;
import fl.flow.facade.flow.condition.SysFlowSHQueryCondition;
import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.FlowCompleteParamsDTO;
import fl.flow.facade.flow.dto.SysFlowSHDTO;
import fl.flow.facade.flow.service.impl.FlowBusnessServiceImpl;
import fl.flow.service.flow.service.ISysFlowSHService;

/**
 * 添加风险保证金流程回调处理接口实现类
 * @author lihaiyang
 *
 */
@Service("assSchFeeDFlowFactoryApiService")
public class AssSchFeeDFlowFactoryApiServiceImpl extends FlowBusnessServiceImpl implements IAssSchFeeDFlowFactoryApi {

	private static final Logger log = LoggerFactory.getLogger(AssSchFeeDFlowFactoryApiServiceImpl.class);
	
	/**
	 * 流程启动历史服务
	 */
	@Autowired
	private ISysFlowSHService sysFlowSHService;
	
	@Autowired
	private AssSchFeeDMapper assSchFeeDMapper;
	
	
	
	
	@Override
	public Map<String, Object> executeBefore(FlowCompleteParamsDTO flowCompleteParamsDTO) throws BaseException {
		String branchKey = flowCompleteParamsDTO.getBranchKey();
		if(StringUtils.isNotEmpty(branchKey)){
			switch (branchKey) {
			case "FLOW_0000_023_001_001_002"://事业部负责人:不通过
				this.noPass(flowCompleteParamsDTO);
				break;
			case "FLOW_0000_023_001_002_002"://事业部总经理:不通过
				this.noPass(flowCompleteParamsDTO);
				break;
			case "FLOW_0000_023_001_003_002"://财务审核不通过
				this.noPass(flowCompleteParamsDTO);
				break;
			case "FLOW_0000_023_001_003_001"://财务审核通过  流程结束:通过
				this.pass(flowCompleteParamsDTO);
				break;
			default:
				break;
			}
		}
		return super.executeBefore(flowCompleteParamsDTO);
	}

	@Override
	public void executeAfter(FlowCompleteParamsDTO flowCompleteParamsDTO) throws BaseException {
		super.executeAfter(flowCompleteParamsDTO);
	}

	@Override
	public String getFlowBusinessActMgrOrgCd(FlowCompleteParamsDTO flowCompleteParamsDTO) throws BaseException {
		SysFlowSHQueryCondition qc = new SysFlowSHQueryCondition();
		qc.setSysCd(flowCompleteParamsDTO.getSysCd());//系统代码
		qc.setWordsInTheTable(PrjBscInfoConstant.PRJ_FLOW_S_H_PRJ);//表中字
		qc.setDataId(flowCompleteParamsDTO.getBusinessKey());;//业务主键
		qc.setpDefKey(flowCompleteParamsDTO.getProcDefKey());
		qc.setStatus(BaseConstant.IS_YESNO_YES);
		List<SysFlowSHDTO> searchList = this.sysFlowSHService.searchList(qc);
		BaseLogUtils.newLogger("已拖回追加费用流程接口回调>>查询流程历史").setKeys("taskId:" + flowCompleteParamsDTO.getTaskId(), "dataId:" + flowCompleteParamsDTO.getBusinessKey()).info();
		if(CollectionUtils.isNotEmpty(searchList)){
			SysFlowSHDTO sysFlowSHDTO = searchList.get(0);
			return sysFlowSHDTO.getOrgCd();
		}
		BaseLogUtils.newLogger("已拖回追加费用流程接口回调>>返回发起人组织架构>>未找到!").info();
		return super.getFlowBusinessActMgrOrgCd(flowCompleteParamsDTO);
	}
	
	/**
	 * 流程结束:通过
	 * @param flowCompleteParamsDTO
	 * @throws BaseException
	 */
	private void pass(FlowCompleteParamsDTO flowCompleteParamsDTO)throws BaseException{
		Long businessKey = flowCompleteParamsDTO.getBusinessKey(); //M 的主键
		Long crtUsrId = flowCompleteParamsDTO.getCrtUsrId();//当前处理人
		/** 1.修改流程状态  */
		AssSchFeeM assSchFeeM = new AssSchFeeM();
		assSchFeeM.setPaySchId(businessKey);
		assSchFeeM.setMdfUsrId(crtUsrId);
		assSchFeeM.setMdfTm(new Date());
		assSchFeeM.setFlowStatusCd(AssFileConstant.FLOW_STATUS_CD_3);
		this.assSchFeeDMapper.updateAssSchFeeM(assSchFeeM);
		
		
		
		/** 2.修改流程历史状态  */
		this.sysFlowSHService.modifyAudStsCd(PrjBscInfoConstant.PRJ_FLOW_S_H_PRJ, FlowConstant.FLOW_CHECK_STATE_PASS, new Date(), new Date(),
				flowCompleteParamsDTO.getCrtUsrId(), flowCompleteParamsDTO.getBusinessKey(), flowCompleteParamsDTO.getProcDefKey(), 
				flowCompleteParamsDTO.getProcDefId(), flowCompleteParamsDTO.getSysCd(), flowCompleteParamsDTO.getProcInstId());
		BaseLogUtils.newLogger("流程通过,修改流程历史状态").setKeys("flowCompleteParamsDTO:" + JSON.toJSONString(flowCompleteParamsDTO)).info();
		/** 3.修改表明细中的 资产状态      */
		PrjPrdDtl prjPrdDtlPO = new PrjPrdDtl();
		prjPrdDtlPO.setPaySchId(businessKey);
		//prjPrdDtlPO.setIsDel(0);
		prjPrdDtlPO.setPrdStsCd(InterfaceConstants.PAY_STATUS_CD_S010602);
		prjPrdDtlPO.setMdfTm(new Date());
		this.assSchFeeDMapper.updatePrjPrdDtl(prjPrdDtlPO);
	}
	/**
	 * 流程结束:不通过
	 * @param flowCompleteParamsDTO
	 * @throws BaseException
	 */
	private void noPass(FlowCompleteParamsDTO flowCompleteParamsDTO)throws BaseException{
		Long businessKey = flowCompleteParamsDTO.getBusinessKey(); //M 的主键
		Long crtUsrId = flowCompleteParamsDTO.getCrtUsrId();//当前处理人
		/**
		 * 1.不通过修改流程状态
		 */
		AssSchFeeM assSchFeeM = new AssSchFeeM();
		assSchFeeM.setPaySchId(businessKey);
		assSchFeeM.setFlowStatusCd(AssFileConstant.FLOW_STATUS_CD_4);
		assSchFeeM.setMdfUsrId(crtUsrId);
		assSchFeeM.setMdfTm(new Date());
		this.assSchFeeDMapper.updateAssSchFeeM(assSchFeeM);
		
		/**2.修改流程历史状态*/
		this.sysFlowSHService.modifyAudStsCd(PrjBscInfoConstant.PRJ_FLOW_S_H_PRJ, FlowConstant.FLOW_CHECK_STATE_NO_PASS, new Date(), new Date(),
				flowCompleteParamsDTO.getCrtUsrId(), flowCompleteParamsDTO.getBusinessKey(), flowCompleteParamsDTO.getProcDefKey(), 
				flowCompleteParamsDTO.getProcDefId(), flowCompleteParamsDTO.getSysCd(), flowCompleteParamsDTO.getProcInstId());
        BaseLogUtils.newLogger("流程不通过,修改流程历史状态").setKeys("flowCompleteParamsDTO:" + JSON.toJSONString(flowCompleteParamsDTO)).info();
		/** 3.修改表明细中的 资产状态      */
		PrjPrdDtl prjPrdDtlPO = new PrjPrdDtl();
		prjPrdDtlPO.setPaySchId(businessKey);
		prjPrdDtlPO.setPrdStsCd(InterfaceConstants.PAY_STATUS_CD_S010602);
		prjPrdDtlPO.setMdfTm(new Date());
		this.assSchFeeDMapper.updatePrjPrdDtl(prjPrdDtlPO);
	
	}
	
	
	

	@Override
	public String executeFlowBack(FlowCompleteParamsDTO flowCompleteParamsDTO) throws BaseException {
		return "";
	}
}
